package com.sh.data.engine.domain.datadev.offline.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sh.data.engine.domain.datadev.offline.model.enums.OfflineDevNodeTypeEnum;
import com.sh.data.engine.domain.datadev.offline.service.DatadevMenuNodeService;
import com.sh.data.engine.domain.datadev.offline.service.DatadevVnodeSerivce;
import com.sh.data.engine.repository.dao.datadev.DatadevVnodeMapper;
import com.sh.data.engine.repository.dao.datadev.entity.DatadevMenuNodeEntity;
import com.sh.data.engine.repository.dao.datadev.entity.DatadevVnodeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DatadevVnodeServiceImpl extends ServiceImpl<DatadevVnodeMapper, DatadevVnodeEntity>
    implements DatadevVnodeSerivce {

    @Lazy
    @Autowired
    private DatadevMenuNodeService datadevMenuNodeService;

    @Override
    public DatadevVnodeEntity getDatadevVnodeByNodeId(Long nodeId) {

        DatadevVnodeEntity one =
            getOne(
                new LambdaQueryWrapper<DatadevVnodeEntity>().eq(DatadevVnodeEntity::getNodeId, nodeId));
        return one;
    }

    @Override
    public DatadevVnodeEntity getDatadevVnodeByFlowId(Long flowId) {
        DatadevMenuNodeEntity one =
            datadevMenuNodeService.getDatadevMenuNodeByFlowId(
                flowId, OfflineDevNodeTypeEnum.VNODE_START);
        Long id = one.getId();
        DatadevVnodeEntity r1 = this.getDatadevVnodeByNodeId(id);
        return r1;
    }

    @Override
    public List<Long> getDatadevVnodeByFlowIds(List<Long> flowIds, List<Integer> cycle) {

        List<Long> res = Lists.newArrayList();
        List<Long> collect = flowIds.stream().sorted().collect(Collectors.toList());
        List<DatadevMenuNodeEntity> datadevMenuNodeEntities =
            datadevMenuNodeService.getDatadevMenuNodeByFlowIds(
                collect, OfflineDevNodeTypeEnum.VNODE_START);

        List<Long> vnodeIds =
            datadevMenuNodeEntities.stream().map(e -> e.getId()).collect(Collectors.toList());
        Map<Long, Long> maps = Maps.newHashMap();
        for (DatadevMenuNodeEntity entity : datadevMenuNodeEntities) {
            maps.put(entity.getId(), entity.getPid());
        }
//    for (int i = 0; i < vnodeIds.size(); i++) {
//      maps.put(vnodeIds.get(i), collect.get(i));
//    }
        List<DatadevVnodeEntity> res1 =
            this.list(
                new LambdaQueryWrapper<DatadevVnodeEntity>()
                    .in(DatadevVnodeEntity::getCycle, cycle)
                    .in(DatadevVnodeEntity::getNodeId, vnodeIds));

        res1.forEach(
            e -> {
                Long aLong = maps.get(e.getNodeId());
                res.add(aLong);
            });
        return res;
    }
}
